package com.cgtong.android.activity;

import android.os.Build;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.LinearLayout;

import com.blankj.utilcode.util.SizeUtils;
import com.cgtong.android.R;
import com.cgtong.android.adapter.CustomFragmentPagerAdapter;
import com.cgtong.android.base.BaseTitleActivity;
import com.cgtong.android.fragment.ProblemFollowedFragment;
import com.cgtong.android.fragment.UserFollowedFragment;
import com.cgtong.android.util.PostUtil;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * 我的关注
 * by zhuqingchun
 */
public class MyFollowsActivity extends BaseTitleActivity {

    @BindView(R.id.tab_layout)
    TabLayout mTabLayout;
    @BindView(R.id.view_pager)
    ViewPager mViewPager;

    @Override
    protected String getContentTitle() {
        return getString(R.string.txt_title_my_followed);
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_my_follows;
    }

    @Override
    protected void loadDataByNet() {
        PostUtil.postSuccessDelayed(loadService);
        initViewPager();

    }

    @Override
    protected void onDataReloadByNet(View v) {

    }


    private void initViewPager() {
        final List<Fragment> fragments = new ArrayList<>();
        String[] titleArray = getResources().getStringArray(R.array.my_follows_array);
        fragments.add(new ProblemFollowedFragment());
        fragments.add(new UserFollowedFragment());
        mViewPager.setAdapter(new CustomFragmentPagerAdapter(getSupportFragmentManager(),fragments,titleArray));
        mTabLayout.setTabMode(TabLayout.MODE_FIXED);
        mTabLayout.setupWithViewPager(mViewPager);
        setUpIndicatorWidth(mTabLayout);
    }


    /**
     * 通过反射修改TabLayout Indicator的宽度（仅在Android 4.2及以上生效）
     */
    private void setUpIndicatorWidth(TabLayout tabLayout) {
        Class<?> tabLayoutClass = tabLayout.getClass();
        Field tabStrip = null;
        try {
            tabStrip = tabLayoutClass.getDeclaredField("mTabStrip");
            tabStrip.setAccessible(true);
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        }

        LinearLayout layout = null;
        try {
            if (tabStrip != null) {
                layout = (LinearLayout) tabStrip.get(tabLayout);
            }
            for (int i = 0; i < layout.getChildCount(); i++) {
                View child = layout.getChildAt(i);
                child.setPadding(0, 0, 0, 0);
                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.MATCH_PARENT, 1);
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
                    params.setMarginStart(SizeUtils.dp2px(45f));
                    params.setMarginEnd(SizeUtils.dp2px(45f));
                }
                child.setLayoutParams(params);
                child.invalidate();
            }
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }
}
